<?php
require_once('class.database.php');

class KartuDao {    

    public function cekKartu($nip){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_kartu WHERE nip='$nip'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

    public function getKartu($id){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_kartu WHERE id='$id'";
        //print_r($query);
        $result=$con->prepare($query);
        $result->execute();
 
        if($result->rowCount()==1){
            $data=$result->fetch();
            return $data;
        }

        return false;
    }

    public function getKartuByNip($nip){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_kartu WHERE nip='$nip'";
        //print_r($query);
        $result=$con->prepare($query);
        $result->execute();
 
        if($result->rowCount()==1){
            $data=$result->fetch();
            return $data;
        }

        return false;
    }

    public function getAllKartu(){
        $db=Database::getInstance();
        $con=$db->getConnection(1);
        $query="SELECT * FROM t_kartu";
        
        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()>=1){
            return $result->fetchAll();
        }

        return false;
    }

    public function createKartu(Kartu $kartu){
        $db= Database::getInstance();
        $con=$db->getConnection(1);

        $tglRekam=date('Y-m-d H:i:s');
        $query="INSERT INTO t_kartu(nip,nomorKartu,tglRekam) ";
        $query.=" VALUES('$kartu->nip','$kartu->nomorKartu','$tglRekam')";

        //print_r($query);
        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

    public function deleteKartu($id){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="DELETE FROM t_kartu WHERE id='$id'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

    public function updateKartu(Kartu $kartu){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="UPDATE t_kartu SET nip='$kartu->nip',nomorKartu='$kartu->nomorKartu'  
        	 WHERE id='$kartu->id'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

}


class Kartu {
	var $id;
    var $nip;
	var $nomorKartu;
    var $tglRekam;
}
?>